﻿using System;
using System.Collections.Generic;
using System.Text;
using Models;
using Repositories;
using Services.IService;
using Repositories.IRepository;
using MySqlSugar;
using System.Linq.Expressions;
using Utility;

namespace Services.Service
{
    public class UsersService : IUsersService
    {

        private readonly IUsersRepository _repository;
        public UsersService(IUsersRepository repository)
        {
            _repository = repository;
        }

        #region 查询

        public UsersModel CheckUser(string loginName, string password)
        {
            Expression<Func<UsersModel, bool>> where = LinqUtil.True<UsersModel>();
            where = where.AndAlso(i => i.Login_name == loginName && i.User_pwd == password);
            return _repository.GetList(where).FirstOrDefault();
        }
        public UsersModel GetByLoginname(string loginname)
        {
            Expression<Func<UsersModel, bool>> where = LinqUtil.True<UsersModel>();
            where = where.AndAlso(i => i.Login_name== loginname);
            return _repository.GetList(where).FirstOrDefault();
        }

        /// <summary>
        /// 查询单条根据主键
        /// </summary>
        /// <returns></returns>
        public UsersModel GetById(int? id)
        {
            return _repository.GetById(id);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<UsersModel> GetList()
        {
            return _repository.GetList().ToList();
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <returns></returns>
        public List<UsersModel> GetList(Expression<Func<UsersModel, bool>> where)
        {
            return _repository.GetList(where).ToList();
        }

        /// <summary>
        /// 按页查询
        /// </summary>
        public List<UsersModel> GetPageList(Expression<Func<UsersModel, bool>> where, int pageIndex, int pageSize, string OrderFields)
        {
            return _repository.GetPageList(where, pageIndex, pageSize, OrderFields).ToList();
        }
        #endregion

        #region 删除
        /// <summary>
        /// 根据主键删除
        /// </summary>
        public bool DeleteById(int id)
        {
            return _repository.DeleteById(id);
        }

        /// <summary>
        /// 根据主键组批量删除
        /// </summary>
        public bool DeleteById(int[] ids)
        {
            return _repository.DeleteById(ids);
        }

        /// <summary>
        /// 根据实体删除【一定要带主键！！！】
        /// </summary>
        public bool DeleteByModel(UsersModel model)
        {
            return _repository.DeleteByModel(model);
        }

        #endregion

        #region 新增
        /// <summary>
        /// 强制新增，不管主键
        /// </summary>
        public int Insert(UsersModel model)
        {
            return _repository.Insert(model);
        }

        /// <summary>
        /// 批量插入 适合海量数据插入
        /// </summary>
        public bool InsertRange(List<UsersModel> list)
        {
            return _repository.InsertRange(list);
        }

        #endregion

        #region 修改
        /// <summary>
        /// 根据实体更新【主键一定要有！！！】
        /// </summary>
        public bool Update(UsersModel model)
        {
            return _repository.Update(model);
        }

        /// <summary>
        /// 10条以上批量更新
        /// </summary>
        public bool UpdateRange(List<UsersModel> list)
        {
            return _repository.UpdateRange(list);
        }
        #endregion
    }
}
